package com.zlh.config;

import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.stereotype.Component;

/**
 * 解决spring bean注入Job的问题
 * Job对象的实例化过程是在Quartz中进行的，这时候将spring的东西注入进来，肯定是行不通的，所以需要这个类
 * @package com.zlh.config
 * @company: dacheng
 * @author: zlh
 * @createDate: 2022/6/14
 */
@Component
public class JobFactory extends AdaptableJobFactory {
    @Autowired
    private AutowireCapableBeanFactory capableBeanFactory;

    @Override
    protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
        // 调用父类的方法
        Object jobInstance = super.createJobInstance(bundle);
        // 注入
        capableBeanFactory.autowireBean(jobInstance);
        return jobInstance;
    }
}
